//
//  CStarts.m
//  HotDisPlus
//
//  Created by yilong zhang on 12-7-17.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "CStarts.h"

@implementation CStarts

@synthesize m_Slider;
@synthesize m_bIsMoved;
@synthesize m_nNum;
@synthesize m_Width;

- (id)initWithFrame:(CGRect)frame Num:(int)num StarSize:(CGSize)starsize
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        m_nNum = num;
        
        //灰色星星
        CGFloat fwidth = frame.size.width/m_nNum;
        m_Width = fwidth;
        UIImageView *star = nil;
        for (int i=0; i<m_nNum; i++)
        {
            star = [[UIImageView alloc] initWithFrame:CGRectMake(i*fwidth, (frame.size.height-starsize.height)/2, starsize.width, starsize.height)];
            star.image = [UIImage imageNamed:@"favorite-gray.png"];
            [self addSubview:star];
            [star release];
        }
        
        m_Slider = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, frame.size.height)];
        m_Slider.clipsToBounds = YES;
        m_Slider.userInteractionEnabled = NO;
        [self addSubview:m_Slider];
        [m_Slider release];
        
        //黄色星星
        for (int i=0; i<m_nNum; i++)
        {
            star = [[UIImageView alloc] initWithFrame:CGRectMake(i*fwidth, (frame.size.height-starsize.height)/2, starsize.width, starsize.height)];
            star.image = [UIImage imageNamed:@"favorite-yellow.png"];
            star.userInteractionEnabled = NO;
            [m_Slider addSubview:star];
            [star release];
        }
    }
    return self;
}

- (void)SetLightStartWithNum:(int)num
{
    num = (num>5) ? (5) : (num);
    m_Slider.frame = CGRectMake(m_Slider.frame.origin.x, m_Slider.frame.origin.y, num*m_Width, m_Slider.frame.size.height);
}

- (void)ResetStart
{
    m_Slider.frame = CGRectMake(m_Slider.frame.origin.x, m_Slider.frame.origin.y, 0, m_Slider.frame.size.height);
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    m_bIsMoved = YES;
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (m_bIsMoved)
    {
        UITouch *touch = [touches anyObject];
        CGPoint point  = [touch locationInView:self];
        if (point.x < self.frame.size.width && point.x > 0)
            m_Slider.frame = CGRectMake(0, 0, point.x, m_Slider.frame.size.height);
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    //int cnt = m_Slider.frame.size.width/(m_Width/2)+0.5;
    int cnt = m_Slider.frame.size.width/m_Width+0.5;
    //m_Slider.frame = CGRectMake(0, 0, m_Width/2*cnt, m_Slider.frame.size.height);
    m_Slider.frame = CGRectMake(0, 0, m_Width*cnt, m_Slider.frame.size.height);
    
    m_bIsMoved = NO;
}

@end
